#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>


int main(int argc, char** argv) {
    if (argc < 2) {
        printf("Usage: client <serverip>");
    }
    int sock = 0;
    sockaddr_in addr;
    if((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
        perror("Unable to create socket");
        return 1;
    }

    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(6001);
    inet_aton(argv[1], (in_addr *)&addr.sin_addr.s_addr);

    char message[256];
    sprintf(message, "My pid: %d", getpid());
    char buf[sizeof(message)];

    sendto(sock, message, sizeof(message), 0, (sockaddr *) & addr, sizeof(addr));
    printf("Sent: %s\n", message);
    recvfrom(sock, buf, sizeof(message), 0, 0, 0);
    printf("Received: %s\n", buf);
    close(sock);
    return 0;
}
